package com.isoftstone.common.base;

import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.util.BocHttpUtil;
import com.isoftstone.util.PropertiesUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


public class BocCommon {

   static   Logger logger = LoggerFactory.getLogger(BocCommon.class);

    public static void checkCard(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Map<String, String>> cardMap = BocHttpUtil.getBocCard(request, "01");

        if (cardMap.size()==0) {

            String bindUrl = PropertiesUtils.getProperties("cardList");

            String CLIENTID = PropertiesUtils.getProperties("clientid");

            String ACCESSTOKEN = (String) request.getSession().getAttribute("accessToken");

            String USERID = (String) request.getSession().getAttribute("userid");


            StringUtils.replace(bindUrl, "CLIENTID", CLIENTID);

            bindUrl =  bindUrl.replace("CLIENTID", CLIENTID).replace("ACCESSTOKEN", ACCESSTOKEN).replace("USERID", USERID);

            response.sendRedirect(bindUrl);
        }
    }


    /**
     * 到中银银商 修改密码页面
     * @param request
     * @param response
     * @throws IOException
     */

    public static void updPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {


            String bindUrl = PropertiesUtils.getProperties("changePswd");

            String CLIENTID = PropertiesUtils.getProperties("clientid");

            String ACCESSTOKEN = (String) request.getSession().getAttribute("accessToken");

            String USERID = (String) request.getSession().getAttribute("userid");


            StringUtils.replace(bindUrl, "CLIENTID", CLIENTID);

            bindUrl =  bindUrl.replace("CLIENTID", CLIENTID).replace("CLIENTSECRET", ACCESSTOKEN).replace("USERID", USERID);

            response.sendRedirect(bindUrl);

        }


    /**
     * 到卡列表页面
     * @param request
     * @param response
     * @throws IOException
     *
     */

    public static void managerCard(HttpServletRequest request, HttpServletResponse response) throws IOException {


        String bindUrl = PropertiesUtils.getProperties("cardList");

        String CLIENTID = PropertiesUtils.getProperties("clientid");

        String ACCESSTOKEN = (String) request.getSession().getAttribute("accessToken");

        String USERID = (String) request.getSession().getAttribute("userid");


        StringUtils.replace(bindUrl, "CLIENTID", CLIENTID);

        bindUrl =  bindUrl.replace("CLIENTID", CLIENTID).replace("ACCESSTOKEN", ACCESSTOKEN).replace("USERID", USERID);


        String url = null;

        /**
         * 是否直接走中银开放平台https地址
         */

        if ("1".equals(BaseController.userContextPathFlag)){
             url =   BaseController.baseContextPath+ "/app/index";
         } else {
             url =  request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/app/index";
         }

        logger.info("===========resetpwdUrl======"+bindUrl+url);

        response.sendRedirect(bindUrl+URLEncoder.encode(url));

    }


    /**
     * 到修改支付密码页面
     * @param request
     * @param response
     * @throws IOException
     *
     */
    public static void toResetPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {


        String resetpwdUrl = PropertiesUtils.getProperties("resetpwd");

        String CLIENTID = PropertiesUtils.getProperties("clientid");

        String ACCESSTOKEN = (String) request.getSession().getAttribute("accessToken");

        String USERID = (String) request.getSession().getAttribute("userid");


        StringUtils.replace(resetpwdUrl, "CLIENTID", CLIENTID);

        resetpwdUrl =  resetpwdUrl.replace("CLIENTID", CLIENTID).replace("ACCESSTOKEN", ACCESSTOKEN).replace("USERID", USERID);

        String url = null;

        if ("1".equals(BaseController.userContextPathFlag)){
            url =  BaseController.baseContextPath + "/app/index";
        } else {
            url =  request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/app/index";
        }

        logger.info("===========resetpwdUrl======"+resetpwdUrl+url);
        response.sendRedirect(resetpwdUrl+ URLEncoder.encode(url));

    }
}
